package com.jed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jed.R;
import com.jed.application.BaseApplication;
import com.jed.util.L;
import com.jed.util.ShareUtil;
import com.jed.util.StaticClass;
import com.jed.util.T;
import com.jed.view.CustomDialog;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 项目名：XMKAPP<br/>
 * 包名：  com.jed.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/27 18:04<br/>
 * 描述：登录<br/>
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        et_username = findViewById(R.id.et_username);
        et_password = findViewById(R.id.et_password);
        String name = ShareUtil.getString("username", "");
        String pass = ShareUtil.getString("password", "");
        boolean isEmpty = !TextUtils.isEmpty(name) && !TextUtils.isEmpty(pass);
        if (isEmpty) {
            et_username.setText(name);
            et_password.setText(pass);
        }
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login://点击登录
                dialog.show();
                final String name = et_username.getText().toString().trim();
                final String pass = et_password.getText().toString().trim();
                boolean isEmpty = !TextUtils.isEmpty(name) && !TextUtils.isEmpty(pass);
                if (isEmpty) {
                    HttpParams params = new HttpParams();
                    params.put("username", name);
                    params.put("password", pass);
                    RxVolley.post(StaticClass.LOGIN_URL, params, new HttpCallback() {

                        @Override
                        public void onSuccess(String t) {
                            dialog.dismiss();
                            L.i("登录请求成功，返回的数据："+t);
                            parseJson(t, name, pass);
                        }

                        @Override
                        public void onFailure(int errorNo, String strMsg) {
                            dialog.dismiss();
                            L.i("登录请求失败，返回的数据errorNo："+errorNo);
                            L.i("登录请求失败，返回的数据strMsg："+strMsg);
                            if (errorNo == -1){
                                T.showLongToast(getResources().getString(R.string.connection_timeout));
                            }
                        }
                    });

                } else {
                    T.showShortToast(this.getString(R.string.input_empty));
                }

                break;
        }
    }

    /**
     * 解析json并保存数据
     */
    private void parseJson(String t, String name, String pass) {
        try {
            JSONObject json = new JSONObject(t);
            String resultType = json.getString("resultType");
            switch (resultType) {
                case "0":
                    startActivity(new Intent(LoginActivity.this, JiCaiListActivity.class));
                    ShareUtil.putString("username", name);
                    ShareUtil.putString("password", pass);
                    break;
                case "101":
                    T.showShortToast(LoginActivity.this.getString(R.string.name_or_password_incorrect));
                    break;
                case "102":
                    T.showShortToast(LoginActivity.this.getString(R.string.request_failed));
                    break;
            }
        } catch (JSONException e) {
            T.showShortToast(LoginActivity.this.getString(R.string.request_failed));
        }
    }
}
